package org.biz.modules.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.biz.modules.entity.*;

import java.util.List;

/**
 * @author zf
 * @ClassName SysUserMapper
 */
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {

    IPage<SysUser> getPage(Page<SysUser> page,@Param("t") QueryParams queryParams);

    @Select("select distinct name from sys_item")
    List<SysItem> getDepartmentList();

    @Select("select id,name,base_hospital_name from sys_item where name=#{name}")
    List<SysItem> getBaseHospitalListByName(@Param("name") String name);


    @Select("select distinct city from sys_hospital")
    List<SysHospital> getCityList();

    List<SysHospital> getHospitalListByName(@Param("cityName") String cityName,@Param("name") String name);

    @Update("update sys_deadline set deadline=#{deadline}")
    int updateDeadline(@Param("deadline") String deadline);

    @Select("select deadline from sys_deadline")
    String getDeadline();


    @Select("select a.id,a.title,count(b.title_id) titleTotal  \n" +
            "from sys_doc_title a \n" +
            "left join sys_doc_title_text b on a.id = b.title_id\n" +
            "group by a.id,a.title")
    List<SysDocTitle> getDocTitleList();


    /**
    *获取用户已填的空
    *
     * @param userId 登录用户id
     * @return java.util.List<org.biz.modules.entity.SysDocUser>
     **/
    List<SysDocUser> getTitleFinishNumByUserId(@Param("userId") String userId);


    List<SysDocTitleText> getTitleText(@Param("titleId") String titleId);


    List<SysUser> getNurseList(@Param("name") String name,@Param("dataYear") String dataYear);

    @Select("SELECT u.idNumber, s.id AS subjectId, s.`name` AS subjectName, u.courseId, c.name AS couseName, IFNULL(ss.isFinish, 0) AS isFinish, CASE WHEN s.hasTest = 1 THEN (CASE WHEN sa.id is not null THEN sa.score >= s.passScore ELSE 0 END) ELSE IFNULL(ss.isFinish, 0) END AS isPass\n" +
            "FROM homecare.sys_user AS u\n" +
            "JOIN homecare.course AS c ON u.courseId = c.id\n" +
            "JOIN homecare.`subject` AS s ON u.courseId = s.courseId\n" +
            "LEFT JOIN homecare.subjectschedule AS ss ON s.id = ss.subjectId AND u.uid = ss.userId\n" +
            "LEFT JOIN homecare.subjectanswersheet AS sa ON s.hasTest = 1 AND sa.state = 1 AND sa.subjectId = ss.subjectId AND u.uid = sa.userId\n" +
            "WHERE u.idNumber = #{idNumber}")
    List<LearningRecord> getLearningRecord(@Param("idNumber") String idNumber);

    List<LearningRecord> getLearningRecordPageList(@Param("idNumberList") List<String> idNumberList);

    @Select("SELECT uid FROM homecare.sys_user WHERE idNumber = #{idNumber}")
    Long selectUidInHomecare(@Param("idNumber") String idNumber);

    void mockLearingRecord(@Param("userId") Long userId, @Param("subjectId") Long subjectId);

    void setSubjectFinish(@Param("userId") Long userId, @Param("subjectId") Long subjectId);

    List<CityUserCount> getCityUserCount();

    @Select("SELECT DISTINCT city FROM sys_hospital where city not in ('外省（非江苏省）','外省') ORDER BY city")
    List<String> getCity13List();
}
